import { Toast } from 'vant'
export const PUBLIC = {
	// 将 Date 转化为指定格式的String
	// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
	// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
	// 例子：
	// dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
	// dateFormat(new Date(), "yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
	//author: meizz
	dateFormat(date, fmt) {
		if(!date) return ''
		var d = typeof date === 'number' || typeof date === 'string' ? new Date(date) : date
		if (!fmt) {
			fmt = "yyyy-MM-dd HH:mm:ss"
		}
		var o = {
			"M+": d.getMonth() + 1, //月份
			"d+": d.getDate(), //日
			"h+": d.getHours() % 12 == 0 ? 12 : d.getHours() % 12, //小时
			"H+": d.getHours(), //小时
			"m+": d.getMinutes(), //分
			"s+": d.getSeconds(), //秒
			"q+": Math.floor((d.getMonth() + 3) / 3), //季度
			S: d.getMilliseconds(), //毫秒
		}
		if (/(y+)/.test(fmt))
			fmt = fmt.replace(
				RegExp.$1,
				(d.getFullYear() + "").substr(4 - RegExp.$1.length)
			)
		for (var k in o)
			if (new RegExp("(" + k + ")").test(fmt))
				fmt = fmt.replace(
					RegExp.$1,
					RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
				)
		return fmt
	},
	formatTime: (timestr) => {
		if (!timestr) return ''
		let year = timestr.substring(0, 4)
		let date = timestr.substring(4, 6) + '-' + timestr.substring(6, 8)
		let time = timestr.substring(8, 10) + ':' + timestr.substring(10, 12)
		let currentD = new Date()
		let currentDay = currentD.getFullYear() + ' ' + (currentD.getMonth() + 1) + '-' + currentD.getDate()
	
		if (new Date(currentDay).getTime() === new Date(year + ' ' + date).getTime()) {
			return time
		} else {
			return date + ' ' + time
		}
	},
	getNowTimeStr() {
		let now = new Date()
		let year = now.getFullYear()
		let month = now.getMonth() + 1
		if (month.toString().length < 2) {
			month = '0' + month
		}
		let date = now.getDate()
		if (date.toString().length < 2) {
			date = '0' + date
		}
		let hours = now.getHours()
		let minutes = now.getMinutes()
		let seconds = now.getSeconds()
		let milliseconds = now.getMilliseconds()
		return '' + year+month+date+hours+minutes+seconds+milliseconds
	},
	/**
 * 提示信息
 */
	message(huiObj,type, msg) {
		if(type === 'success') {
			Toast.success(msg)
		} else {
			Toast.fail(msg)
		}
	},
	// 防抖
	debounce(fn, delay = 100) {
		let timer;
		return function () {
			clearTimeout(timer);
			timer = setTimeout(() => {
				try {
					fn.apply(this, arguments);
				} catch (e) {
					console.error("debouce error:", e);
				}
			}, delay);
		};
	},
	// 获取url中的某个参数
	getParamFromURL(url, paramName) {
		const urlParams = url.split('?')[1]; // 获取URL中'?'后面的部分
		if (!urlParams) return null; // 如果没有查询参数，则返回null
	
		const paramsArray = urlParams.split('&'); // 将参数字符串按'&'分割成数组
		for (let i = 0; i < paramsArray.length; i++) {
			const param = paramsArray[i].split('='); // 再将每个键值对按'='分割
			if (decodeURIComponent(param[0]) === paramName) { // 检查是否为所需参数名
				return decodeURIComponent(param[1]); // 返回对应的参数值（已解码）
			}
		}
		return null; // 如果未找到参数，则返回null
	}
}


//生成uuid的方法
export function guid() {
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
			var r = Math.random() * 16 | 0,
					v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
	});
}


/**
* 文本转字节流
* @param {*} s 
* @returns 
*/
export function textToArrayBuffer(s) {
	var i = s.length;
	var n = 0;
	var ba = new Array()
	for (var j = 0; j < i;) {
		var c = s.codePointAt(j);
		if (c < 128) {
			ba[n++] = c;
			j++;
		}
		else if ((c > 127) && (c < 2048)) {
			ba[n++] = (c >> 6) | 192;
			ba[n++] = (c & 63) | 128;
			j++;
		}
		else if ((c > 2047) && (c < 65536)) {
			ba[n++] = (c >> 12) | 224;
			ba[n++] = ((c >> 6) & 63) | 128;
			ba[n++] = (c & 63) | 128;
			j++;
		}
		else {
			ba[n++] = (c >> 18) | 240;
			ba[n++] = ((c >> 12) & 63) | 128;
			ba[n++] = ((c >> 6) & 63) | 128;
			ba[n++] = (c & 63) | 128;
			j+=2;
		}
	}
	return new Uint8Array(ba).buffer;
}